<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <script type='text/javascript' src='media/js/quadraMask.js'></script>
            <script type='text/javascript' src='media/js/validateMultipleNumber.js'></script>

            <h2 class="title">Pesquisar Imóvel</h2><br/>
            <p:growl id="messages" showDetail="false" autoUpdate="true"  /> 

            <h:form id="form" enctype="multipart/form-data" target="_blank">

                <div class="buttonsActionImovel" style="width: 465px">
                    <div style="float: right">
                        <p:splitButton value="Opções" icon="ui-icon-gear" update="@form">  

                            <p:menuitem value="Relação Simplificada" icon="ui-icon-print" ajax="false"
                                        action="#{managerPesquisarImovel.relatorioRelacaoImoveisSimplificado()}"/>

                            <p:menuitem value="Área Cessão" icon="ui-icon-print" ajax="false"
                                        action="#{managerPesquisarImovel.relatorioPendenciaAreaCessao}"/>
                        </p:splitButton> 
                    </div>
                </div>

                <p:panel id="pesquisaAvancadaPanel" styleClass="container_24 clearfix" header="Pesquisa Avançada" toggleable="true" collapsed="true" 
                         rendered="#{!managerPesquisarImovel.soResultado}">

                    <h4 class="title">Imóvel</h4>

                    <p:outputLabel value="Inclusão" for="tipoInclusao" styleClass="grid_3"/>
                    <p:selectBooleanCheckbox id="tipoInclusao" value="#{managerPesquisarImovel.inclusao}" styleClass="grid_2">
                        <p:ajax update="@this, :form:tipoAlteracao" listener="#{managerPesquisarImovel.atualizarTipoRegistro(true)}" />
                    </p:selectBooleanCheckbox>

                    <p:outputLabel value="Alteração" for="tipoAlteracao" styleClass="grid_3"/>
                    <p:selectBooleanCheckbox id="tipoAlteracao" value="#{managerPesquisarImovel.alteracao}" styleClass="grid_2">
                        <p:ajax update="@this, :form:tipoInclusao" listener="#{managerPesquisarImovel.atualizarTipoRegistro(false)}" />
                    </p:selectBooleanCheckbox>

                    <p:outputLabel value="Situação" styleClass="grid_3"/>
                    <div class="grid_6">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovel.situacaoImovel}">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="EM_BRANCO" itemLabel="EM BRANCO"/>
                            <f:selectItems value="#{managerUtilitario.situacaoImovel}"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <p:outputLabel value="Caracterização do Imóvel" styleClass="grid_3"/>
                    <div class="grid_6">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovel.caracterizacao}">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="EM_BRANCO" itemLabel="EM BRANCO"/>
                            <f:selectItems value="#{managerUtilitario.caracterizacao}"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Uso do Imóvel" styleClass="grid_4"/>
                    <div class="grid_6">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovel.usoImovel}">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItems value="#{managerUtilitario.usoImovel}"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Proprietário</h4>
                    <p:outputLabel value="Proprietário" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComProprietario}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Renda familiar" styleClass="grid_4" for="tempoMoradia"/>
                    <div class="grid_5">
                        <p:selectOneMenu value="#{managerPesquisarImovel.proprietario.rendaFamiliar}" panelStyle="width: 215px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItems value="#{managerUtilitario.rendaFamiliar}"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <p:outputLabel value="Tempo moradia" styleClass="grid_3" for="tempoMoradia"/>
                    <div class="grid_4">
                        <p:inputText id="tempoMoradia" value="#{managerPesquisarImovel.proprietario.tempoMoradia}" style="width: 100%"/>
                    </div>

                    <p:outputLabel value="Tempo moradia municipio" styleClass="grid_4" for="tempoMoradiaMunicipio"/>
                    <p:inputText id="tempoMoradiaMunicipio" styleClass="grid_4" value="#{managerPesquisarImovel.proprietario.tempoMoradiaMunicipio}"/>
                    <div class="clear"/>

                    <h4 class="title">Endereço</h4>
                    <p:outputLabel value="Número" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComNumero}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Anexos</h4>

                    <p:outputLabel value="Anexo" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComAnexo}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Croqui" styleClass="grid_4"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComCroqui}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Dimensões e Confrontções</h4>

                    <p:outputLabel value="Dimensão" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComDimensoes}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Confrontação" styleClass="grid_4"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComConfrontacao}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Coordenadas" styleClass="grid_4"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerPesquisarImovel.imovelComCoordenadasDimensoes}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                </p:panel>

                <p:panel id="fatorPanel" styleClass="container_24 clearfix" rendered="#{!managerPesquisarImovel.soResultado}">

                    <h4 class="title">Matrícula</h4>

                    <p:outputLabel value="Número" styleClass="grid_3" for="matriculaInputText" />
                    <p:inputText id="matriculaInputText" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.imovel.id}" />
                    <div class="clear-both"/><br/>

                    <h4 class="title">Fator Localização</h4>

                    <p:outputLabel value="Inscrição Imóvel" styleClass="grid_4 inscricaoLabelImovel" for="maskinscricao" 
                                   rendered="false"/>
                    <p:inputMask mask="?**.**.***.****.**" id="maskinscricao" rendered="false"
                                 styleClass="grid_6"  value="#{managerPesquisarImovel.visaoInscricao}"/>
                    <p:watermark value="DD.SS.QUADR.LOTE.UN" for="maskinscricao" />

                    <div class="clear"/>

                    <p:outputLabel value="Distrito" styleClass="grid_3" for="dInput" />
                    <p:inputText id="dInput" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.imovel.inscricaoCadastralD}" />

                    <p:outputLabel value="Setor" styleClass="grid_3" for="sInput" />
                    <p:inputText id="sInput" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.imovel.inscricaoCadastralS}" />

                    <p:outputLabel value="Quadra" styleClass="grid_3" for="qInput" />
                    <p:inputText id="qInput" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.inscricaoCadastralQuadra}"
                                 onkeydown="jQuery(this).validateMultipleNumber();"/>
                    <p:watermark value="1,2;3-5" for="qInput" rendered="true"/>
                    <div class="clear-both" />

                    <p:outputLabel value="Lote" styleClass="grid_3" for="lInput" />
                    <p:inputText id="lInput" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.imovel.inscricaoCadastralLote}" />

                    <p:outputLabel value="Unidade" styleClass="grid_3" for="uInput" />
                    <p:inputText id="uInput" styleClass="grid_4" 
                                 value="#{managerPesquisarImovel.imovel.inscricaoCadastralUnid}" />
                    <div class="clear-both" />
                </p:panel>


                <p:panel id="ownerPanel" styleClass="container_24 clearfix"  rendered="#{!managerPesquisarImovel.soResultado}">
                    <h4 class="title">Proprietário</h4>

                    <p:outputLabel value="Proprietário" styleClass="grid_3 proprietarioLabelPesquisar"/>
                    <p:selectOneRadio id="typePerson" 
                                      value="#{managerPesquisarImovel.proprietario.tipoDocumento}" >  
                        <f:selectItems value="#{managerUtilitario.tipoDocumento}" />
                        <p:ajax event="change" update=":form:ownerPanel" />
                    </p:selectOneRadio>

                    <div class="clear"/>

                    <p:outputLabel id="typeDocumentLabel" 
                                   value="#{managerPesquisarImovel.proprietario.tipoDocumento}: " 
                                   styleClass="grid_3"/>

                    <f:validateBean disabled="true">
                        <p:inputMask id="typeDocumentInput" 
                                     value="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                              managerPesquisarImovel.proprietario.cpf : managerPesquisarImovel.proprietario.cnpj}"
                                     mask="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                             '999.999.999-99':'99.999.999/9999-99'}" 
                                     styleClass="grid_5" > 

                        </p:inputMask>
                    </f:validateBean>
                    <div class="clear"/>
                    <p:outputLabel value="Nome: " styleClass="grid_3" for="nomeInputText" 
                                   rendered="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CPF'}"/>
                    <p:inputText id="nomeInputText" styleClass="grid_19" 
                                 value="#{managerPesquisarImovel.proprietario.nome}" 
                                 rendered="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                    <p:outputLabel value="Fantasia: " styleClass="grid_3" for="nameFantasyInputText" 
                                   rendered="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                    <p:inputText id="nameFantasyInputText" styleClass="grid_19"
                                 value="#{managerPesquisarImovel.proprietario.nomeFantasia}" 
                                 rendered="#{managerPesquisarImovel.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />

                </p:panel>

                <p:panel id="addressPanel" styleClass="container_24 clearfix"  rendered="#{!managerPesquisarImovel.soResultado}">

                    <h4 class="title">Endereço</h4>

                    <p:outputLabel value="CEP" styleClass="grid_3" for="cepInputMask" />
                    <p:inputMask id="cepInputMask" styleClass="grid_4" mask="99.999-999"
                                 value="#{managerPesquisarImovel.endereco.cep}" />
                    <div class="clear-both" />

                    <p:outputLabel value="Número" styleClass="grid_3" for="numInputText" />
                    <p:inputText id="numInputText" styleClass="grid_2"
                                 value="#{managerPesquisarImovel.endereco.numero}" />

                    <p:outputLabel value="Logradouro" styleClass="grid_4 alpha" for="patioInputText" />
                    <p:inputText id="patioInputText" styleClass="grid_13"
                                 value="#{managerPesquisarImovel.endereco.logradouro}" />

                    <div class="clear-both" />
                    <p:outputLabel value="Bairro" styleClass="grid_3" for="neighborhoodInputText" />
                    <p:inputText id="neighborhoodInputText" styleClass="grid_19"
                                 value="#{managerPesquisarImovel.endereco.bairro}" />

                </p:panel>

                <br/>
                <div class="buttonAction">
                    <p:commandButton id="pesquisarCommandButton" value="Pesquisar" icon="ui-icon-search"
                                     update="tableImovel"  rendered="#{!managerPesquisarImovel.soResultado}"
                                     actionListener="#{managerPesquisarImovel.pesquisarImovel()}"
                                     styleClass="ui-priority-primary" />
                    <p:commandButton id="bt_clear" value="Limpar" icon="ui-icon-clean"
                                     rendered="#{!managerPesquisarImovel.soResultado}"
                                     actionListener="#{managerPesquisarImovel.limpar()}"
                                     update="@form" process="@this" />
                </div >
                <br/><br/>

                <p:blockUI id="BLOCK"
                           block="resultPanel" trigger="pesquisarCommandButton, tableImovel" > 
                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                </p:blockUI>

                <h4 class="title"  style="#{managerPesquisarImovel.soResultado ? 'margin-top: -60px;':''}" >Resultado</h4>
                <p:panel id="resultPanel" styleClass="container_24 clearfix withoutBorder">

                    <!--                    <p:dataTable id="tableImovel" var="item" value="{managerPesquisarImovel.dataModel}"
                                                     selection="{managerPesquisarImovel.imovelSelected}"
                                                     styleClass="grid_24" rowIndexVar="rowid" 
                                                     rowKey="{item.id}"
                                                     style="white-space: pre-wrap;" 
                                                     binding="{managerPesquisarImovel.searchResults}"
                                                     rows="10" paginator="true" lazy="true"
                                                     paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                                                     currentPageReportTemplate="{currentPage} de {totalPages}"
                                                     paginatorPosition="bottom" emptyMessage="Nenhum registro">-->
                    <p:dataTable id="tableImovel" var="item" value="#{managerPesquisarImovel.imoveis}"
                                 selection="#{managerPesquisarImovel.imovelSelected}"
                                 styleClass="grid_24" rowIndexVar="rowid" 
                                 rowKey="#{item.id}" rowsPerPageTemplate="5,10,15"
                                 style="white-space: pre-wrap;" 
                                 rows="10" paginator="true"
                                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {RowsPerPageDropdown} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                                 currentPageReportTemplate="{currentPage} de {totalPages}"
                                 paginatorPosition="bottom" emptyMessage="Nenhum registro">

                        <!--<p:outputLabel value="Imóvel {managerPesquisarImovel.searchResults.rowCount eq 0 ? '' : '('.concat(managerPesquisarImovel.searchResults.rowCount).concat(')')}" />-->
                        <f:facet name="header">
                            <p:outputLabel value="Imóvel #{managerPesquisarImovel.imoveis.isEmpty() ? '' : '('.concat(managerPesquisarImovel.imoveis.size()).concat(')')}" />
                        </f:facet>

                        <p:column rendered="#{managerPesquisarImovel.vReg ne null}" selectionMode="single" disabledSelection="#{item.comando.valor eq 1}"/>

                        <p:column headerText="Ação" width="50">
                            <p:button title="Editar" rendered="false"
                                      href="editarimovel.xhtml?eImovel=#{item.id}"
                                      icon="ui-icon-pencil"/>
                            <p:button title="Visualizar"
                                      href="visualizarimovel.xhtml?vImovel=#{item.id}"
                                      icon="ui-icon-zoomin"/>

                            <p:commandButton update="@form" title="Excluir"
                                             oncomplete="dlg.show()"
                                             icon="ui-icon-trash">
                                <f:setPropertyActionListener target="#{managerPesquisarImovel.imovelDelete}" value="#{item}" />
                            </p:commandButton>

                        </p:column>

                        <p:column headerText="Inscrição Cadastral" width="200" style="white-space: pre-wrap;">
                            <h:outputText value="#{managerPesquisarImovel.inscricaoCadastral(item)}" />
                        </p:column>
                        <p:column headerText="Localização" width="350" style="white-space: pre-wrap;">
                            <h:outputText value="#{item.enderecoImovel.logradouro ne null ? item.enderecoImovel.logradouro : item.enderecoImovel.nomeclog}, Nº #{item.enderecoImovel.numero}" />
                        </p:column>
                        <p:column headerText="Bairro" width="350" style="white-space: pre-wrap;">  
                            <h:outputText value="#{item.enderecoImovel.bairroDne ne null and 
                                                   item.enderecoImovel.bairroDne.nome ne null and !item.enderecoImovel.bairroDne.nome.isEmpty() ?
                                                   item.enderecoImovel.bairroDne.nome : item.enderecoImovel.bairro}"/>
                        </p:column> 
                        <p:column headerText="Proprietário" width="450" style="white-space: pre-wrap;">  
                            <h:outputText style="#{managerPesquisarImovel.proprietarios(item) eq '' ? 'color: red;' : ''}"
                                          value="#{managerPesquisarImovel.proprietarios(item) eq '' ? 'Proprietário Pendente' : managerPesquisarImovel.proprietarios(item)}"/>
                        </p:column> 

                        <p:column headerText="Documento" width="350" style="white-space: pre-wrap;">  
                            <h:outputText style="#{managerPesquisarImovel.proprietariosDocumento(item) eq '' ? 'color: red;' : ''}"
                                          value="#{managerPesquisarImovel.proprietariosDocumento(item) eq '' ? 'Documento Pendente' : managerPesquisarImovel.proprietariosDocumento(item)}"/>
                        </p:column>

                        <p:column headerText="Tipo Registro" style="white-space: pre-wrap;" rendered="#{managerPesquisarImovel.soResultado}">  
                            <h:outputText value="#{item.comando.nome}"/>
                        </p:column> 
                    </p:dataTable> 

                </p:panel>

                <br/><br/>
                <div class="buttonAction">
                    <p:commandButton id="mergeButton" value="Atualizar" 
                                     rendered="#{managerPesquisarImovel.vReg ne null}"
                                     process="tableImovel, @this" actionListener="#{managerPesquisarImovel.redirectAtualizarCadastro()}"
                                     styleClass="ui-priority-primary" />
                </div >

                <p:dialog id="modalDialog" header="Confirmação de remoção" widgetVar="dlg" modal="true" 
                          height="80" closable="false" resizable="false" closeOnEscape="true">
                    <h:outputText value="Tem certeza que deseja remover este imóvel ?" />
                    <div class="buttonAction">
                        <p:commandButton value="Sim"
                                         actionListener="#{managerPesquisarImovel.remover()}"
                                         update="@form"
                                         icon="ui-icon-check"/>
                        <p:commandButton value="Não" icon="ui-icon-close"
                                         oncomplete="dlg.hide()" />
                    </div>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>
</html>
